#include "nfc.h"



#include <qnearfieldtarget.h>
#include <QNearFieldManager>
#include <qndefmessage.h>
#include <qndefrecord.h>
#include <qndefnfctextrecord.h>
#include <qndefnfcurirecord.h>

#include <QDebug>

NFC::NFC(QWidget *parent) :
    QWidget(parent),m_manager(new QNearFieldManager(this))
{
    qDebug()<<"In constructor of NFC manager";
    connect(m_manager, SIGNAL(targetDetected(QNearFieldTarget*)),
            this, SLOT(targetDetected(QNearFieldTarget*)));

    connect(m_manager, SIGNAL(targetLost(QNearFieldTarget*)),
            this, SLOT(targetLost(QNearFieldTarget*)));
}

void NFC::targetDetected(const QNdefMessage &message, QNearFieldTarget *target)
{
    Q_UNUSED(target);
    qDebug()<<"NFC Target detected";
    displayNdefMessage(message);
}

void NFC::targetDetected(QNearFieldTarget *target)
{
    qDebug()<<"NFC Target detected 1";
    qDebug()<<"UID ="<<target->uid().toHex();
    emit sendUid(target->uid());

//    if (!target->hasNdefMessage())
//        return;



    connect(target, SIGNAL(ndefMessageRead(QNdefMessage)),
            this, SLOT(displayNdefMessage(QNdefMessage)));
    connect(target, SIGNAL(error(QNearFieldTarget::Error,QNearFieldTarget::RequestId)),
            this, SLOT(targetError(QNearFieldTarget::Error,QNearFieldTarget::RequestId)));

    target->readNdefMessages();
}

void NFC::targetLost(QNearFieldTarget *target)
{
    target->deleteLater();
}


void NFC::startListing()
{
    m_manager->startTargetDetection(QNearFieldTarget::AnyTarget);
}

void NFC::stopListning()
{
    m_manager->stopTargetDetection();
}

void NFC::displayNdefMessage(const QNdefMessage &message)
{


    qDebug()<<"NFC display message";
     QByteArray mArray = message.toByteArray();
     emit sendNDFMSG(mArray);
    qDebug()<<mArray;




}


void NFC::targetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id)
{
    Q_UNUSED(error);
    Q_UNUSED(id);


        switch (error) {
        case QNearFieldTarget::NoError:

            emit sendError("");
            break;
        case QNearFieldTarget::UnsupportedError:
            emit sendError("Unsupported tag");

            break;
        case QNearFieldTarget::TargetOutOfRangeError:
            emit sendError("No response from tag");

            break;
        case QNearFieldTarget::NoResponseError:
            emit sendError("No response from tag");

            break;
        case QNearFieldTarget::ChecksumMismatchError:
            emit sendError("Checksum mismatch");

            break;
        case QNearFieldTarget::InvalidParametersError:
            emit sendError("Invalid parameters");

            break;
        case QNearFieldTarget::NdefReadError:
            emit sendError("NDEF read error");

            break;
        case QNearFieldTarget::NdefWriteError:
            emit sendError("NDEF write error");

            break;
        default:
            emit sendError("Unknown error");



    }
}
